//
//  AccountOrderListVC.m
//  ImGuider
//
//  Created by llt on 2017/5/12.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "AccountOrderListVC.h"

#import "AccountOrderListCell.h"
#import "LinePreDetailVC.h"
#import "ContactService.h"

#import "RatingVC.h"

@interface AccountOrderListVC ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSArray *dataSource;

//@property (nonatomic) NSInteger start;

@end

@implementation AccountOrderListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setup];
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//
//    if ([IGUtil hasLoginOrPresentLogin]) {
//
//
//    }
//}
//- (void)viewWillDisappear:(BOOL)animated {
//
//    [super viewWillDisappear:animated];
//
//
//}

- (void)setup {
    
    [self configEmptyDataTableView:self.tableView emptyImageName:@"暂无订单" describe:MYLocalizedString(@"PUBLIC_NODATA_ORDER", nil) refresh:nil block:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadDataSource) name:kLoginSuccessNotif object:nil];
    
    @weakify(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
//        self.start = 0;
        [self loadDataSource];
    }];
    
//    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        @strongify(self);
//        [self loadDataSource];
//    }];
    
    [self.tableView setThemeStyle:ThemeStyleBlack];

    if (![AppManager sharedManager].hasTicket) {
        self.superVC = self;
    }
    
    if ([IGUtil hasLoginOrPresentLogin]) {
        
        [ActivityIndicatorView showHUDAddedTo:self.view];
        //        self.start = 0;
        [self loadDataSource];
    }
}


//- (NSMutableArray *)dataSource {
//
//    if (!_dataSource) {
//        _dataSource = [NSMutableArray array];
//    }
//    return _dataSource;
//}

- (void)loadDataSource {
    
    @weakify(self);
    [NetAPIManager getOrderStart:@(0) count:@(1000) type:MerchTypeLine Block:^(NSArray<OrderModel *> *orderArray, NSError *error) {
        @strongify(self);
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        
        self.dataHasLoad = YES;
        [self.tableView.mj_header endRefreshing];
        
//        if (self.start == 0) {
//            [self.tableView.mj_footer resetNoMoreData];
//            [self.dataSource removeAllObjects];
//        }
        
//        if (orderArray.count < 10) {
//            [self.tableView.mj_footer endRefreshingWithNoMoreData];
//        } else {
//
//            [self.tableView.mj_footer endRefreshing];
//        }
        
        self.dataSource = orderArray;
        
//        [self.dataSource addObjectsFromArray:orderArray];
        
        [self.tableView reloadData];
        
//        self.start ++;
    }];
}

#pragma mark -UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *identifi = @"cell1";
    
    
    OrderModel *order = self.dataSource[indexPath.section];
    
    if (order.orderstatus.integerValue == -1) {// 失败
        identifi = @"cell2";
    }
    
    AccountOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifi];
    
    cell.evaluateBtn.tag = indexPath.section;
    cell.contactServiceBtn.tag = indexPath.section;
    [cell.evaluateBtn addTarget:self action:@selector(evaluateAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contactServiceBtn addTarget:self action:@selector(contactServiceAction:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.order = order;
    
    return cell;
}

- (void)evaluateAction:(UIButton *)sender {
    
    OrderModel *order = self.dataSource[sender.tag];
    
    RatingVC *rateVC = [RatingVC new];
    rateVC.isLine = YES;
    rateVC.relationid = order.unionid;
    
    [rateVC show:self.superVC completeBlock:^(CGFloat score, NSString *comment) {
        
        [IGUtil showToast:MYLocalizedString(@"PUBLIC_COMMIT_SUCCESS", nil)];
        order.score = @(score).stringValue;
        [self.tableView reloadData];
    }];
    
}
- (void)contactServiceAction:(UIButton *)sender {
    
    UIViewController *contactService = [IGUtil loadViewController:@"ContactService" storyboard:@"Account"];
    [self.superVC.navigationController pushViewController:contactService animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    OrderModel *order = self.dataSource[indexPath.section];
    
    
    LinePreDetailVC *detailVC = [IGUtil loadViewController:@"LinePreDetailVC" storyboard:@"LineDetail"];
    detailVC.lineid = order.lineid;
    
    detailVC.hidesBottomBarWhenPushed = YES;
    
    
    [self.superVC.navigationController pushViewController:detailVC animated:YES];
    
}
@end

